package com.taylor.BaiYueGu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taylor.BaiYueGu.entity.EduCourse;
import com.taylor.BaiYueGu.entity.EduTeacher;
import com.taylor.BaiYueGu.service.EduCourseService;
import com.taylor.BaiYueGu.service.EduTeacherService;
import com.taylor.BaiYueGu.vo.EduTeacherVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Taylor
 * @since 2021-01-16
 */

@Api(tags = "讲师api")
@RestController
@RequestMapping("/eduTeacher")
@CrossOrigin(value = "*",maxAge = 7200)
public class EduTeacherController {

    /**
     * 查询所有讲师
     */
    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/get/teachers")
    public List<EduTeacher> getTeacher() {
        List<EduTeacher> teachers = teacherService.list();
        return teachers;
    }
    /**
     * 分页查询讲师列表
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("page/{current}/{limit}")
    public Map<String,Object> queryTeacherByPage(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable("current") Long current,
            @ApiParam(name = "limit", value = "多少条", required = true)
            @PathVariable("limit")Long limit){

        Page<EduTeacher> resourcePage = new Page<>(current,limit);
        teacherService.page(resourcePage, null);
        long total = resourcePage.getSize();
        List<EduTeacher> teachers = resourcePage.getRecords();
        Map map = new HashMap(4);
        map.put("ResultCode", 200);
        map.put("total", total);
        map.put("teachers", teachers);

        return map;
    }

    /**
     * 多条件查询
     * @param current
     * @param limit
     * @param teacherVO
     * @return
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Map queryTeacherByTeacherVO(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable("current") Long current,
            @ApiParam(name = "limit", value = "多少条", required = true)
                                        @PathVariable("limit")Long limit,
            @ApiParam(name = "teacherVO", value = "根据条件查询教师")
            @RequestBody(required = false) EduTeacherVo teacherVO){
        Map result = teacherService.queryTeacherByTeacherVO(current,limit,teacherVO);
        return result;
    }
    /**
     * 添加讲师信息
     * @param teacher
     * @return
     */
    @PostMapping("addTeacher")
    @ApiOperation(value = "添加讲师")
    public Map addTeacher(@RequestBody EduTeacher teacher){
        boolean saveResult = teacherService.save(teacher);
        Map map = new HashMap(2);
        return (Map) (saveResult ? map.put("resultCode",200) : map.put("resultCode",Integer.parseInt("!"+200)));
    }

    /**
     * 根据id逻辑删除 讲师信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public Map removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id){
        boolean flag = teacherService.removeById(id);
        Map map = new HashMap(2);
        return (Map) (flag ? map.put("resultCode",200) : map.put("resultCode",Integer.parseInt("!"+200)));
    }

    @GetMapping("queryTeacher/{id}")
    public Map<String, Object> queryById(@PathVariable("id") String id) {
        EduTeacher teacher = teacherService.getId(id);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", id);

        List<EduCourse> courselist = courseService.list(wrapper);
        Map<String, Object> map = new HashMap<>(2);
        map.put("ReturnCode",200);
        map.put("teacher", teacher);
        map.put("course",courselist);
        return map;
    }
}

